<?php


namespace app\school\controller;

use app\common\controller\BasicAdmin;
use app\common\traits\SelectN;
use app\common\traits\SelectPage;
use app\common\traits\TableSelect;
use library\tools\Data;
use think\Db;
use think\Model;

/**
 * Class CustomerCate
 * @package app\sunshine\controller
 */
class CustomerCate extends BasicAdmin
{
    use SelectPage;
    use TableSelect;
    use SelectN;
    /**
     * 绑定数据表
     * @var string
     */
    protected $model = 'CustomerCateModel';
    protected $table_search = 'name';

    public function index()
    {
        $this->title = '客片分类管理';
        return  $this->_query($this->model)
            ->where(function ($db){
                if($this->request->has('name','get') && !empty($this->request->get('name'))){
                    $name = $this->request->get('name');
                    $db->where('name', 'like', '%' . $name . '%');
                }
            })
            ->equal('status')
            ->order('sort asc,id desc')
            ->page(false);
    }


    /**
     * 添加分类信息
     * @return mixed
     */
    public function add()
    {
        $this->title = '添加分类';
        return $this->_form($this->model, 'form');
    }

    /**
     * 编辑分类信息
     * @return mixed
     */
    public function edit()
    {
        $this->title = '编辑分类';
        return $this->_form($this->model, 'form');
    }

	public function _add_form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $this->alterInfo($data);
        }
    }
	
	public function _edit_form_filter(&$data)
	{
	    if ($this->request->isPost()) {
	        $this->alterInfo($data);
	    }
	}
	
	protected function alterInfo(&$data){
	    if(empty($data['image'])){
	        $this->error('图片不能为空！');
	    }
	}
	
    public function upstatus(){
        $this->_save($this->model, [
            'status' => $this->request->request('status'),
        ],'id', ['id' => $this->request->request('id')]);
    }

	public function upindex(){
        $this->_save($this->model, [
            'show_index' => $this->request->request('show_index'),
        ],'id', ['id' => $this->request->request('id')]);
    }
	
    /**
     * 删除分类
     */
    public function del()
    {
        $this->_delete($this->model);
    }
}